<template>
  <el-dialog
    title="修改密码"
    :visible.sync="dialogVisible"
    width="30%"
    :close-on-click-modal="false"
    :before-close="close"
  >
    <div>
      <el-form
        ref="RefForm"
        :model="formData"
        label-width="80px"
        :rules="formRules"
        status-icon
      >
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="formData.newPassword" show-password></el-input>
        </el-form-item>
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input v-model="formData.oldPassword" show-password></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button
        :loading="loadingSubmit"
        type="primary"
        @click="changePassword"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import dialogEditMixin from '@/mixins/dialogEdit'
  import { eleValidator } from '@/utils/tools'
  export default {
    mixins: [dialogEditMixin],
    data() {
      return {
        formRules: {
          newPassword: [
            { required: true, message: '请输入标题', trigger: 'blur' },
            {
              validator: eleValidator([
                { rule: 'isPassword', msg: '只能输入5-16位数字、字母、下划线的组合' }
              ]),
              trigger: 'blur'
            }
          ],
          oldPassword: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            {
              validator: eleValidator([
                { rule: 'isPassword', msg: '只能输入5-16位数字、字母、下划线的组合' }
              ]),
              trigger: 'blur'
            }
          ]
        },
        apiKey: {
          search: 'user/search',
          add: 'user/add',
          put: 'user/put'
        }
      }
    },
    methods: {
      resetFormData() {
        this.formData = {
          newPassword: '',
          oldPassword: ''
        }
      },
      async changePassword() {
        const id = this.formData.id
        delete this.formData.id
        console.log(this.formData)
        let res = await this.$api['user/changePassword'](
          {
            newPassword: this.formData.newPassword,
            oldPassword: this.formData.oldPassword
          },
          { apiParams: { id } }
        )
        console.log(res)
      }
    }
  }
</script>

<style lang="scss" scoped></style>
